﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web.DynamicData;
using NotAClue.Web;

namespace DD_Extensions
{
    public partial class ValuesList_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // set field options
            this.SetFieldOptions();

            // add CSS from meta data
            this.ApplyCss();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SetUpValidator(RequiredFieldValidator1);

            // set validators to use image as error indicator
            this.SetValidatorsToImage();

            if (DropDownList1.Items.Count == 0)
            {
                if (Mode == DataBoundControlMode.Insert && Column.IsRequired)
                    DropDownList1.Items.Add(new ListItem("[Select one]", ""));
                else if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
                    DropDownList1.Items.Add(new ListItem("[Not Set]", ""));

                PopulateListControl(DropDownList1);
            }

            var value = Request.QueryString[Column.Name];
            if (Mode == DataBoundControlMode.Insert && !String.IsNullOrEmpty(value))
            {
                var item = DropDownList1.Items.FindByValue(value);
                if (item != null)
                    DropDownList1.SelectedValue = value;
            }
        }

        /// <summary>
        /// Populates a list control with all the values from a parent table.
        /// </summary>
        /// <param name="listControl">The list control to populate.</param>
        protected new void PopulateListControl(ListControl listControl)
        {
            var attribute = Column.GetAttributeOrDefault<ValuesListAttribute>().Values;
            listControl.Items.AddRange(attribute);
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            if (Mode == DataBoundControlMode.Edit && FieldValue != null)
            {
                var item = DropDownList1.Items.FindByValue(FieldValueString);
                if (item != null)
                    DropDownList1.SelectedValue = FieldValueString;
            }
        }

        /// <summary>
        /// Provides dictionary access to all data in the current row.
        /// </summary>
        /// <param name="dictionary">The dictionary that contains all the new values.</param>
        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            string value = DropDownList1.SelectedValue;
            if (value == String.Empty)
                value = null;

            dictionary[Column.Name] = ConvertEditedValue(value);
        }

        /// <summary>
        /// Gets the data control that handles the data field in a field template.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// A data control that handles the data field in a field template.
        /// </returns>
        public override Control DataControl
        {
            get { return DropDownList1; }
        }
    }
}